package cn.iocoder.yudao.module.cdk.service.cdkuser;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.cdk.controller.admin.cdkuser.vo.CdkuserPageReqVO;
import cn.iocoder.yudao.module.cdk.controller.admin.cdkuser.vo.CdkuserSaveReqVO;
import cn.iocoder.yudao.module.cdk.dal.dataobject.cdkuser.CdkuserDO;
import cn.iocoder.yudao.module.cdk.dal.mysql.cdkuser.CdkuserMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.cdk.enums.ErrorCodeConstants.USER_NOT_EXISTS;

/**
 * cdk使用用户 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CdkuserServiceImpl implements CdkuserService {

    @Resource
    private CdkuserMapper userMapper;

    @Override
    public Integer createuser(CdkuserSaveReqVO createReqVO) {
        // 插入
        CdkuserDO user = BeanUtils.toBean(createReqVO, CdkuserDO.class);
        userMapper.insert(user);
        // 返回
        return user.getId();
    }

    @Override
    public void updateuser(CdkuserSaveReqVO updateReqVO) {
        // 校验存在
        validateuserExists(updateReqVO.getId());
        // 更新
        CdkuserDO updateObj = BeanUtils.toBean(updateReqVO, CdkuserDO.class);
        userMapper.updateById(updateObj);
    }

    @Override
    public void deleteuser(Integer id) {
        // 校验存在
        validateuserExists(id);
        // 删除
        userMapper.deleteById(id);
    }

    private void validateuserExists(Integer id) {
        if (userMapper.selectById(id) == null) {
            throw exception(USER_NOT_EXISTS);
        }
    }

    @Override
    public CdkuserDO getuser(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public PageResult<CdkuserDO> getuserPage(CdkuserPageReqVO pageReqVO) {
        return userMapper.selectPage(pageReqVO);
    }

    @Override
    public CdkuserDO find(String id) {
        return null;
    }

}